package com.learning.spring.cloud.ms.consumer.movie.config;

import com.learning.spring.cloud.ms.consumer.movie.feign.UserFeignService;
import feign.Client;
import feign.Contract;
import feign.Feign;
import feign.auth.BasicAuthRequestInterceptor;
import feign.codec.Decoder;
import feign.codec.Encoder;
import org.springframework.cloud.netflix.feign.FeignClientsConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
 * ClassName: FeignConfig
 * Description:
 * <p>
 * 1. 手动创建Feign，不需要添加注解 @EnableFeignClients、@FeignClient
 * 2. 依赖 FeignClientsConfiguration
 * <p>
 * Date: 2019/6/4 17:40 【需求编号】
 *
 * @author Sam Sho
 * @version V1.0.0
 */
@Import(FeignClientsConfiguration.class)
@Configuration
public class FeignConfig {

    @Bean
    public UserFeignService userUserFeignService(Decoder decoder, Encoder encoder, Client client, Contract contract) {
        // 这边的decoder、encoder、client、contract，可以debug看看是什么实例。
        return Feign.builder()
                .client(client)
                .encoder(encoder)
                .decoder(decoder)
                .contract(contract)
                .requestInterceptor(new BasicAuthRequestInterceptor("user", "user"))
                .target(UserFeignService.class, "http://ms-provider-user/");
    }


    @Bean
    public UserFeignService adminUserFeignService(Decoder decoder, Encoder encoder, Client client, Contract contract) {
        // 这边的decoder、encoder、client、contract，可以debug看看是什么实例。
        return Feign.builder()
                .client(client)
                .encoder(encoder)
                .decoder(decoder)
                .contract(contract)
                .requestInterceptor(new BasicAuthRequestInterceptor("admin", "admin"))
                .target(UserFeignService.class, "http://ms-provider-user/");
    }
}
